/*
before: this = original elements

success: this = original elements 

value:
	this = original elements
	@param elem = current element checking its value

error:
	this = elements returned from value callback (if returned elements) OR original elements
	@param elem = current element having an error
	
cleanup:
	this = elements returned from error callback
	no params


complete (and before):
	called ONCE for EACH rule, after all elements have been checked
	this = elements checked
	@param errors = Object w/ errors
			errors.elems = elements with errors
			errors.msgs = error messages

*/

$(function() {

// add new callbacks
	$.user_input('complete','default',function(errors) {
		if(errors.elems.length) {
			errors.elems.eq(0).trigger('focus');
		}
	});
		

// new rule set
	$.user_input('rule','associate_fields', {
		value: function(elem) { // must return TRUE and nothing else
			var elems = this,
				values = [],
				r = true;

			elems.each(function(i) {
				var val = $(this).val();
				
				if($.inArray(val,values) === -1) {
					values.push(val); }

				if(i === elems.length - 1 && values.length > 1) {
					r = values;	}
			});
			return r;
		},
		event: 'submit',
		cleanup: 'remove',
		error: function(elem,rule){
			// only show an error for the last element
			// also build a string of titles for each element
			var st = this.data('some_string') || [];
			
			if(this[ this.length - 1 ] == elem[0]) { // last
				//console.warn(rule)
				return $('<p class="error">this must be the same as '+st.join(', ')+'</p>')
							.appendTo(elem.parents('.grid-row'));
			} else { // others
				st.push( elem.attr('name') )
			}
			
			this.data('some_string',st);
		}
	});

	$('.required').user_input({ // rule
		value: 'required', // text matches existing value rule | regexp or fn matches value
		new_value: function(el,value) { return value.toUpperCase(); },
		success: function(el) {
			setTimeout(function() {
				el.val( el.val().toLowerCase() ); // back to lower case
			}, 1000);
		},
		event: ['blur','submit'],
		error: function(elem){
			return	$('<p class="error">this is required</p>')
				.appendTo(elem.parents('.grid-row'));
		},
		cleanup: 'slideUp'
	});
	
	$('#txtPassNew,#txtPassRetype,#txtPassNewSomeOther').user_input('associate_fields',{ // extend rule
		before: function(){
			//console.warn('About to validate',this,arguments)
		},
		some_other_data: true
	});
	
	$('#txtPassNew,#txtPassRetype').user_input({
		event: ['blur','submit'], 
		value: /(^\d{10,}$|^$)/, // 'at least 10 numbers',
		error: function(el) {
			el.css('background-color','red');
						
			return el.attr('name')
	
		},
		success: function(el){
			el.css('background-color','');
		},
		complete: function(errors){
			if(errors.elems.length) {
				console.error( errors.msgs.join(', ') + ' must be at least 10 numbers' )
			}
		}
	});
	
	$('#txtPassCurrent').user_input({
		value: 'email',
		event: ['keydown','blur'],
		error: function(el) {
			el.css('background-color','yellow');
		},
		success: function(el){
			el.css('background-color','');
		}
	});
	
	
});